package com.ui.mine.view;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.base.BaseRefreshFragment;
import com.bean.ToDoBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ddd.wandemo.R;
import com.ui.mine.adapter.TodoListRvAdapter;
import com.ui.mine.presenter.TodoFragmentContract;
import com.ui.mine.presenter.TodoFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

public class TodoFragment extends BaseRefreshFragment<TodoFragmentPresenter> implements TodoFragmentContract.View {
    private Toolbar mToolbar;
    private List<ToDoBean.DatasBean> datas = new ArrayList<>();
    private TodoListRvAdapter mAdapter;
    private RecyclerView mRvUserTodo;
    private SwipeRefreshLayout mSrlTodo;
    private int page = 1;

    @Override
    protected TodoFragmentPresenter initPresenter() {
        return new TodoFragmentPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_user_todo;
    }

    @Override
    protected void initEventAndData() {
        //刷新回调
        mSrlTodo.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                mPresenter.getTodoList(-1, -1, page);
            }
        });
        //加载回调
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getTodoList(-1, -1, ++page);
            }
        }, mRvUserTodo);
    }

    @Override
    protected void initView() {
        mRvUserTodo = view.findViewById(R.id.rv_user_todo);
        mSrlTodo = view.findViewById(R.id.srl_todo);

        mSrlTodo.setColorSchemeColors(((TodoDetailActivity)getActivity()).mAccentColor);
        mRvUserTodo.setLayoutManager(new LinearLayoutManager(mContext));

        mAdapter = new TodoListRvAdapter(R.layout.item_todo_done, datas);
        mRvUserTodo.setAdapter(mAdapter);
    }

    @Override
    public void onGetTodo(ToDoBean bean) {
        mAdapter.setNewData(bean.getDatas());
        //首次进入不走loadmore回调
        mAdapter.disableLoadMoreIfNotFullPage();
    }

    @Override
    public void onLoadMore(ToDoBean bean) {
        if (bean.getDatas().size() == 0) {
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.addData(bean.getDatas());
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    protected void loadData() {
        mPresenter.getTodoList(-1, -1, page);
    }
}
